/**
 * @file boards\gd32f103cb\bsp\bsp_rtt.c
 *
 * Copyright (C) 2023
 *
 * bsp_rtt.c is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author HinsShum hinsshum@qq.com
 *
 * @encoding utf-8
 */

/*---------- includes ----------*/
#include "SEGGER_RTT.h"
#include "serial.h"


/*---------- macro ----------*/
/*---------- type define ----------*/
/*---------- variable prototype ----------*/
/*---------- function prototype ----------*/
static bool bsp_init(void);
static uint16_t bsp_write(uint8_t* pbuf, uint16_t len);

/*---------- variable ----------*/
static serial_describe_t rtt = { .baudrate = -1,
                                .comport = -1,
                                .ops.init = bsp_init,
                                .ops.deinit = NULL,
                                .ops.dir_change = NULL,
                                .ops.write = bsp_write,
                                .ops.irq_handler = NULL };
DEVICE_DEFINED(rtt, serial, &rtt);

/*---------- function ----------*/
static bool bsp_init(void) {
    SEGGER_RTT_Init();

    return true;
}

static uint16_t bsp_write(uint8_t* pbuf, uint16_t len) {
    SEGGER_RTT_Write(0, (void*)pbuf, len);

    return len;
}
